package com.comcast.core.eeg.eventpublication.service.ws;

import javax.ejb.EJBException;
import javax.ejb.EJBLocalHome;
import javax.ejb.EJBLocalObject;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.comcast.core.eeg.eventpublication.ejb.EventPublicationServiceLocal;
import com.comcast.core.eeg.eventpublication.ejb.EventPublicationServiceLocalHome;
import com.comcast.core.eeg.eventpublication.service.Constants;
import com.comcast.core.eeg.eventpublication.types.PublicationOption;
import com.comcast.core.eeg.types.BusinessEvent;
import com.comcast.core.eeg.types.ErrorMessage;
import com.comcast.core.eeg.types.Event;
import com.comcast.core.eeg.types.Participant;
import com.comcast.core.eeg.types.Result;

/**
 * Wrapper of EventPublicationServiceLocal local ejb object
 * 
 * 
 * @author thuang000c
 *
 */
public class EventPublicationServiceLocalImpl implements EventPublicationServiceLocal {
    private static Log logger = LogFactory.getLog(EventPublicationServiceLocalImpl.class);
    
    //delay initialization
    private Object sigLock = new Object();
    
    //defined in web.xml
    private String jndiName = "java:comp/env/ejb/EventPublicationService";
    
    private EventPublicationServiceLocal localService;
    
    public void setJndiName(String value) {
        jndiName = value;
    }
    
    public Result publishEvent(Participant arg0, Event arg1, PublicationOption arg2) { 
        synchronized (sigLock) {
            if (localService == null) {
                Result result = init();
                if (result != null) {
                    return result;
                }
            }
        }        
        return localService.publishEvent(arg0, arg1, arg2);
    }

    public Result publishEvents(Participant arg0, Event[] arg1, PublicationOption arg2) {  
        synchronized (sigLock) {
            if (localService == null) {
                Result result = init();
                if (result != null) {
                    return result;
                }
            }
        }        
        return localService.publishEvents(arg0, arg1, arg2);
    }
    
    private Result init() {
        Context context = null;
        Result result = null;
        try {
            context = new InitialContext();
            Object object = context.lookup(jndiName);
            /* pay attention! no narrow'ing!!! */
            EventPublicationServiceLocalHome home = (EventPublicationServiceLocalHome)object;
            localService = home.create(); 
            //ejb 3.0
            //localService = (EventPublicationServiceLocal)object;
        } catch (Exception e) {
            logger.fatal("Failed to initialize EventPublicationServiceLocal.", e);
            result = new Result();
            result.setSucceeded(false);
            result.setErrors(new ErrorMessage[]{new ErrorMessage(Constants.EVENTPUBLICATION_1007, null)});            
        } finally {
            if (context != null) { //security context - propagate from WS to EJB?
                try {
                    context.close();
                } catch (Exception e){
                    logger.warn(e);
                }               
            }
        }
        return result;
    }
    
    ////////////////////////////////////////////
    // NOT supported
    /////////////////////////////////////////////

    public Result batchPublish(Participant arg0, BusinessEvent[] arg1, PublicationOption arg2) {        
        return null;
    }

    public Result publish(Participant arg0, BusinessEvent arg1, PublicationOption arg2) {
        // TODO Auto-generated method stub
        return null;
    }

    public EJBLocalHome getEJBLocalHome() throws EJBException {
        // TODO Auto-generated method stub
        return null;
    }

    public Object getPrimaryKey() throws EJBException {
        // TODO Auto-generated method stub
        return null;
    }

    public boolean isIdentical(EJBLocalObject arg0) throws EJBException {
        // TODO Auto-generated method stub
        return false;
    }

    public void remove() throws RemoveException, EJBException {
        // TODO Auto-generated method stub
        
    }
}
